/*
 * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
 *
 * This software is distribuable under the GNU Lesser General Public License.
 * For more information visit gnu.org.
 */
package org.emergent.antbite.savant.ant.types;


import java.io.File;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.DataType;

import org.emergent.antbite.savant.Artifact;
import org.emergent.antbite.savant.LocalCacheStore;
import org.emergent.antbite.savant.Process;
import org.emergent.antbite.savant.SavantException;
import org.emergent.antbite.savant.Workflow;


/**
 * <p>
 * This class is the workflow that is used when attemping to
 * fetch artifacts.
 * </p>
 *
 * @author  Brian Pontarelli
 */
public class WorkflowType extends DataType {

    private Workflow proxy = new Workflow();


    /**
     * Constructs a new <code>WorkflowType</code>.
     */
    public WorkflowType() {
    }


    /**
     * Returns the Workflow object that this object is a proxy to.
     */
    public Workflow getProxy() {
        return proxy;
    }

    /**
     * Add a new process to the list of processes that compose this workflow.
     *
     * @param   process The new process
     */
    public void add(DataType process) {
        Process p;
        if (process instanceof ProcessType) {
            p = ((ProcessType) process).getProxy();
        } else if (process instanceof Process) {
            p = (Process) process;
        } else {
            throw new BuildException("WorkflowType only supports nested tasks that " +
                "implement org.emergent.antbite.savant.Process");
        }

        proxy.addProcess(p);
        log("Added process to workflow [" + process.getClass().getName() + "]",
            Project.MSG_DEBUG);
    }

    /**
     * Loops over all the processes until the artifact is found or not.
     *
     * @param   artifact The artifact to locate
     * @param   localCache The local cache where the artifact is cached
     * @return  The artifact if found or null
     */
    public File findArtifact(Artifact artifact, LocalCacheStore localCache)
    throws SavantException {
        log("Looking for artifact [" + artifact + "]", Project.MSG_DEBUG);
        return proxy.findArtifact(artifact, localCache);
    }
}